import * as ScreenShort from "js-web-screen-shot";
class AzScreenShort{
    screenShotHandler: {};
    constructor() {
        this.screenShotHandler = {};
        this.init();
    }
    init() {
        this.screenShotHandler = new ScreenShort({
            enableWebRtc: false, // 是否显示选项框
            level: 99, // 层级
            completeCallback: (base64data: string) => {
                this.completeCallback(base64data);
            },
            closeCallback: this.closeCallback
        });
    }
    /**
     * 根据图片生成画布
     */
    convertImageToCanvas(image: any) {
        var canvas: HTMLCanvasElement | any = document.createElement("canvas");
        canvas.width = image.width;
        canvas.height = image.height;
        canvas.getContext("2d").drawImage(image, 0, 0);
        return canvas;
    }
    /**
     * 生成图片
     * 截图确认按钮回调函数
     */
    completeCallback(base64data: string) {
        var image = new Image();
        var self = this;
        image.src = base64data;
        image.onload = () => {
          var canvas = self.convertImageToCanvas(image);
          var url = canvas.toDataURL("image/jpeg");
          var bytes = window.atob(url.split(",")[1]);  //通过atob将base64进行编码
          //处理异常，将ASCII码小于0的转换为大于0,进行二进制转换
          var buffer = new ArrayBuffer(bytes.length);
          var uint = new Uint8Array(buffer);  //生成一个8位数的数组
          for (var i = 0; i < bytes.length; i++) {
            uint[i] = bytes.charCodeAt(i);  //根据长度返回相对应的Unicode 编码
          }
          //Blob对象
          var imageFile= new Blob([buffer], { type: "image/jpeg" }); //type为图片的格式
          // 给后台返回的数组，我的项目需求是要截图多张上传给后端的，如果只上传一张直接就上传imageFile上一句就行，这个数组可以跟根据自己的需求进行编写
          console.log('imageFile', imageFile);
          const result = new File([imageFile], '截图', {type: 'image/jpeg', lastModified: Date.now()})
          console.log('result', result);
        }
    }
    closeCallback() {
    }
};
export default AzScreenShort;
